package itheima04;
/*
    1.定义Runnable接口的实现类
    2.Runnable接口的实现类覆盖重写run方法,指定线程任务: 卖票
        卖票步骤:
            (1)判断是否有票
            (2)有: 出一张票
            (3)减少一张票
 */
/*public class MyTickets implements Runnable{
    //因为100张票,被3个线程共享使用,所以定义在成员位置
    private int tickets = 100;
    @Override
    public void run() {
        //死循环
        while(true) {
            //(1)判断是否有票
            if(tickets>0) {
                try {
                    Thread.sleep(30);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                //(2)有: 出一张票
                System.out.println("窗口: "+Thread.currentThread().getName()+" 卖出第: "+tickets+" 张票....");
                //(3)减少一张票
                tickets--;
            }
        }
    }
}*/

public class  MyTickets implements Runnable{
    private  int tickets=100;
    @Override
    public void run(){
        while (tickets>0){
            try{
                Thread.sleep(30);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("窗口:"+Thread.currentThread().getName()+"卖出第："+tickets+"张票");
                tickets--;
        }
    }

}